/*================================================================
*   File Name：db.c
*   Mail: a1277992967@163.com 
*   blog: http://www.ycst.club/suggest/ 
*   Author：ycst
*   Created Time：2022年08月16日 星期二 15时15分35秒
*   Describe：
*
================================================================*/

#include "../include/db.h"

int abc = 220;
MYSQL *g_conn = NULL;

//insert into dht11_data_record values(0, '123', '2', '3', '4', now());

int DHT11SaveData(int argc, char *argv[]){
#if 1
	if(argv == NULL){
		printf("argv = NULL\r\n");
		return -1;
	}
	printf("g_conn = %p\r\n", g_conn);
	printf("abc = %d\r\n", abc);
	
	char buf[200] = {0};
	int res = 0;
	strcpy(buf, "insert into dht11_data_record(device_id, device_type, humidity, temperature, record_time) values('");	
	strcat(buf, argv[0]);
	strcat(buf, "', '");
	strcat(buf, argv[1]);
	strcat(buf, "', '");
	strcat(buf, argv[2]);
	strcat(buf, "', '");
	strcat(buf, argv[3]);
	strcat(buf, "', ");
		
	strcat(buf, "now())");
	
	printf("mysql语句 : %s 长度：%ld \r\n", buf, strlen(buf));
#endif	
	res = mysql_query(g_conn, buf);
	if(res == 0)	printf("数据执行成功\n");
	else 	printf("数据执行失败\n");
	
}


int DHT11updateDeviceInfo(int argc, char *argv[]){
	if(argv == NULL){
		printf("argv = NULL\r\n");
		return -1;
	}
	char buf[200] = {0};
	int res = 0;
	strcpy(buf, "update dht11_device_record set record_time = now() WHERE device_id = '");
	strcat(buf, argv[0]);
	strcat(buf, "'");
	
	printf("mysql语句 : %s \r\n", buf);
	res = mysql_query(g_conn, buf);
	if(res == 0)	printf("数据执行成功\n");
	else 	printf("数据执行失败\n");
	
    return 0;
}

int DHT11SaveDeviceInfo(int argc, char *argv[]){
	if(argv == NULL){
		printf("argv = NULL\r\n");
		return -1;
	}
	char buf[200] = {0};
	int res = 0;
	strcpy(buf, "insert into dht11_device_record values (");
	strcat(buf, argv[0]);
	strcat(buf, ", now())");
	
	printf("mysql语句 : %s \r\n", buf);
	
	res = mysql_query(g_conn, buf);
	if(res == 0)	printf("数据执行成功\n");
	else 	printf("数据执行失败\n");
	
    return 0;
}
int DHT11SaveCmdResult(int argc, char *argv[]){
	if(argv == NULL){
		printf("argv = NULL\r\n");
		return -1;
	}
	char buf[200] = {0};
	int res = 0;
	strcpy(buf, "insert into dht11_cmd_record(device_id, device_type, major_cmd, min_cmd, cmd_result, record_time) values('");	
	strcat(buf, argv[0]);
	strcat(buf, "', '");
	strcat(buf, argv[1]);
	strcat(buf, "', '");
	strcat(buf, argv[2]);
	strcat(buf, "', '");
	strcat(buf, argv[3]);
	strcat(buf, "', '");
	strcat(buf, argv[4]);
	strcat(buf, "', ");
	
	strcat(buf, "now());");
	
	printf("mysql语句 : %s \r\n", buf);
	res = mysql_query(g_conn, buf);
	if(res == 0)	printf("数据执行成功\n");
	else 	printf("数据执行失败\n");
}
int InitMysql(){
	abc = 233;
	g_conn=NULL;
    //初始化mysql句柄
    g_conn = mysql_init(NULL);
    if(g_conn != NULL){
        printf("初始化mysql句柄成功\n");
    }

    //打开指定的数据库
    if(mysql_real_connect(g_conn,NULL,"root","ycst","dht11",0,NULL,0)!=NULL ){
        printf("数据库连接成功\n");
        printf("g_conn = %p\r\n", g_conn);
        return 0;
    }
    else{
        printf("数据库连接失败\n");
        return -1;
    }
    
}
void CloseMySql(){
	mysql_close(g_conn);
}
